//
//  WSCommentViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/11/10.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSCommentViewController.h"

@interface WSCommentViewController ()<UITextFieldDelegate>

// 输入框
@property (weak, nonatomic) IBOutlet UITextField *inputText;

// 底部工具条
@property (weak, nonatomic) IBOutlet UIView *toolBar;


@end

@implementation WSCommentViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_inputText becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_inputText resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    [kNotificationCenter addObserver:self selector:@selector(keyBoardWillShow:)
//                                name:UIKeyboardWillShowNotification object:nil];
//    
//    [kNotificationCenter addObserver:self selector:@selector(keyBoardWillHide:)
//                                name:UIKeyboardWillHideNotification object:nil];
    
        
    [self.inputText setRoundBorder:kSeparatorColor radius:3.f];

}


#pragma mark - 监听键盘

// 键盘升起
- (void)keyBoardWillShow:(NSNotification *)noti {
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:noti.userInfo];
    // 获取键盘高度
    CGRect keyBoardBounds  = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    NSLog(@"height is %f", keyBoardHeight);
    
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.toolBar.transform = CGAffineTransformMakeTranslation(0, -(keyBoardHeight + 50));
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
}

// 键盘下降
- (void)keyBoardWillHide:(NSNotification *)noti {
    
}


// 点击屏幕
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITextFieldDelegate

// 点击发送按钮
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self dismissViewControllerAnimated:YES completion:nil];
    if (![textField hasText]) return NO;
    
    if (self.sendMsgCallback) {
        self.sendMsgCallback(textField.text);
    }
    
    return YES;
}


#pragma mark - 移除通知

- (void)dealloc {
    [kNotificationCenter removeObserver:self];
}

@end
